﻿using FunPlus.Api.Common;
using FunPlus.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace FunPlus.Api.PayWeixin
{
    public class Weixin
    {
        public static string PreUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

        public static string GetRequestBody(SortedDictionary<string, string> sParaTemp)
        {
            var dic = BuildRequestPara(sParaTemp);
            return StringParaToXml(CreateLinkString(dic));
        }

        public static Dictionary<string, string> BuildRequestPara(SortedDictionary<string, string> sParaTemp)
        {
            //待签名请求参数数组
            Dictionary<string, string> sPara = new Dictionary<string, string>();
            //签名结果
            string mysign = "";

            //过滤签名参数数组
            sPara = FilterPara(sParaTemp);

            sPara.Add("key", AppConfig.wx_api_key);

            //获得签名结果
            mysign = BuildRequestMysign(sPara);

            //签名结果与签名方式加入请求提交参数组中
            sPara.Add("sign", mysign);
            sPara.Remove("key");

            return sPara;
        }

        public static string GetMysign(SortedDictionary<string, string> sParaTemp)
        {
            //待签名请求参数数组
            Dictionary<string, string> sPara = new Dictionary<string, string>();
            string mysign = "";
            //过滤签名参数数组
            sPara = FilterPara(sParaTemp);
            sPara.Add("key", AppConfig.wx_api_key);
            //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
            string prestr = CreateLinkString(sPara);

            //把最终的字符串签名，获得签名结果

            mysign = CommonHelper.GetMD5(prestr);
            return mysign;
        }
        private static string BuildRequestMysign(Dictionary<string, string> sPara)
        {
            //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
            string prestr = CreateLinkString(sPara);

            //把最终的字符串签名，获得签名结果
            string mysign = "";
            mysign = CommonHelper.GetMD5(prestr);
            return mysign;
        }

        private static string CreateLinkString(Dictionary<string, string> dicArray)
        {
            StringBuilder prestr = new StringBuilder();
            foreach (KeyValuePair<string, string> temp in dicArray)
            {
                prestr.Append(temp.Key + "=" + temp.Value + "&");
            }

            //去掉最後一個&字符
            int nLen = prestr.Length;
            prestr.Remove(nLen - 1, 1);

            return prestr.ToString();
        }

        /// <summary>
        /// 除去数组中的空值和签名参数并以字母a到z的顺序排序
        /// </summary>
        /// <param name="dicArrayPre">过滤前的参数组</param>
        /// <returns>过滤后的参数组</returns>
        private static Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre)
        {
            Dictionary<string, string> dicArray = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> temp in dicArrayPre)
            {
                if (temp.Key.ToLower() != "sign" && temp.Value != "" && temp.Value != null)
                {
                    dicArray.Add(temp.Key, temp.Value);
                }
            }

            return dicArray;
        }

        public static string StringParaToXml(string strPara)
        {
            StringBuilder sbXml = new StringBuilder();
            sbXml.Append("<xml>");
            var items = strPara.Split('&');
            foreach (string item in items)
            {
                var kv = item.Split('=');
                sbXml.AppendFormat("<{0}>{1}</{0}>", kv[0], kv[1]);
            }

            sbXml.Append("</xml>");
            return sbXml.ToString();
        }

        public static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        public static int GetTimestamp()
        {
            return ConvertDateTimeInt(DateTime.Now);
        }
    }
}
